const app = getApp()
import util from '../../utils/util.js'
import json from '../../assets/jobs.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activId: '1',
    myList: [],
    checkedId: '',
    bindex: '',
    delBtnWidth: 320,
    startX: '',
    inputText: ''
  },
  onLoad: function(options) {
    let temp = json.json
    let myList = []
    temp.forEach((item, index) => {
      myList.push({
        title: item,
        id: index + 1,
        color: util.color(),
        startTime: util.formatTime(new Date())
      })
    })
    this.setData({
      myList
    })
  },

  onShow: function() {},

  // ---------------------------
  showDeleteButton: function(e) {
    let productIndex = e.currentTarget.dataset.productindex
    this.setXmove(productIndex, -160)
  },
  hideDeleteButton: function(e) {
    let productIndex = e.currentTarget.dataset.productindex

    this.setXmove(productIndex, 0)
  },
  handleTouchStart(e) {
    this.startX = e.touches[0].pageX
  },
  setXmove: function(productIndex, xmove) {
    let myList = this.data.myList
    myList[productIndex].xmove = xmove
    this.setData({
      myList
    })
  },
  handleMovableChange: function(e) {
    if (e.detail.source === 'friction') {
      if (e.detail.x < -30) {
        this.showDeleteButton(e)
      } else {
        this.hideDeleteButton(e)
      }
    } else if (e.detail.source === 'out-of-bounds' && e.detail.x === 0) {
      this.hideDeleteButton(e)
    }
  },
  getVal(e) {
    console.log('e', e.detail.value)
    this.setData({
      inputText: e.detail.value
    })
  },
  addArr() {
    let tempList = this.data.myList
    tempList.unshift({
      title: {
        name: this.data.inputText,
        company:{
          name: `测试公司${tempList.length + 1}`
        }
      },
      id: tempList.length + 1,
      color: util.color(),
      startTime: util.formatTime(new Date())
    })
    console.log('tempList', tempList)
    this.setData({
      myList: tempList,
      inputText: ''
    })
  },
  delCalendar(e) {
    console.log('e', e)
    let id = e.currentTarget.dataset.id
    let tempList = this.data.myList
    tempList = tempList.filter(item => {
      return item.id !== id
    })
    this.setData({
      myList: tempList
    })
  },
  edit(e) {
    let id = e.currentTarget.dataset.id
    console.log('id', id)
    let tempList = this.data.myList
    let aaa = tempList.find(item => {
      console.log('item', item.id)
      return item.id == id
    })
    aaa.title = '11'
    console.log('aaa', aaa)
    this.setData({
      myList: tempList
    })
  }
})
